<?php

namespace app\admin\model;

use think\Model;

class Tags extends Model
{
    // 表名
    protected $name = 'tags';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'group_text',
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public static function getTagsByType($type = 0,$company_id = 0){
        //分组:0=类型,1=级别,2=状态

        //公司是否单独设置标签
        if($company_id){
            $res = self::get(['company_id' => $company_id]);
            if($res){
                $data = self::all(['company_id' => $company_id,'group' => $type]);
                $return = [];
                foreach($data as $k => $v){
                    $v->toArray();

//                    $return[$v['id']] = $v['name'];
                    $return[$k]['id'] = $v['id'];
                    $return[$k]['name'] = $v['name'];
                }
                return $return;
            }
        }

        $data = self::all(['group' => $type,'company_id' => 0]);
        $return = [];
        foreach($data as $k => $v){
            $v->toArray();
//            $return[$v['id']] = $v['name'];
            $return[$k]['id'] = $v['id'];
            $return[$k]['name'] = $v['name'];
        }
        return $return;
    }

    
    public function getGroupList()
    {
        return ['0' => __('Group 0'),'1' => __('Group 1'),'2' => __('Group 2')];
    }     

    public function getStatusList()
    {
        return ['1' => __('Status 1'),'0' => __('Status 0')];
    }     


    public function getGroupTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['group']) ? $data['group'] : '');
        $list = $this->getGroupList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




}
